/*
 * @(#)XfsimInitDialog.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsim.view;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import xfuzzy.xfsim.model.*;
import xfuzzy.xfsim.view.listener.*;
import javax.swing.*;
import java.awt.*;

/**
 * Di�logo para introducir los valores iniciales
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfsimInitDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603057L;
	
	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Ventana principal de la aplicaci�n
	 */
	private Xfsim xfsim;
	
	/**
	 * Campos para introducir los valores iniciales
	 */
	private XTextForm text[];
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//
	
	/**
	 * Constructor
	 */
	public XfsimInitDialog(Xfsim xfsim) {
		super(xfsim,"Xfsim",true);
		this.xfsim = xfsim;
		
		Variable input[] = xfsim.getSpecification().getSystemModule().getInputs();
		text = new XTextForm[input.length];
		for(int i=0; i<text.length; i++) {
			text[i] = new XTextForm(input[i].getName());
			text[i].setFieldWidth(200);
		}
		XTextForm.setWidth(text);
		
		XfsimConfig config = xfsim.getConfiguration();
		double[] init = config.getPlantInit();
		if(init != null)
			for(int i=0; i<text.length && i<init.length; i++)
				text[i].setText(""+init[i]);
		
		String lb[] = { "Set", "Unset", "Cancel" };
		XCommandForm form = new XCommandForm(lb,lb,new XfsimInitActionListener(this));
		form.setCommandWidth(100);
		
		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Initial plant values"));
		for(int i=0; i<text.length; i++) content.add(text[i]);
		content.add(form);
		
		Point loc = xfsim.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		this.setLocation(loc);
		pack();
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//
		
	/**
	 * Seleciona los valor iniciales de las variables
	 */
	public void set() {
		boolean good = true;
		double value[] = new double[text.length];
		for(int i=0; i<text.length; i++) {
			try { value[i] = Double.parseDouble(text[i].getText()); }
			catch (NumberFormatException e) { good = false; text[i].setText(""); }
		}
		if(good) { xfsim.getConfiguration().setPlantInit(value); setVisible(false); }
		else Toolkit.getDefaultToolkit().beep(); 
	}
	
	/**
	 * Deselecciona los valores iniciales de las variables
	 */
	public void unset() {
		xfsim.getConfiguration().setPlantInit(null);
		setVisible(false);
	}
}

